package com.mmall.admin.controller;

import com.mmall.admin.pojo.param.AddUserParam;
import com.mmall.admin.pojo.param.UserLoginParam;
import com.mmall.admin.service.AdminService;
import com.mmall.admin.shiro.ShiroOperation;
import com.mmall.admin.shiro.ShiroUser;
import com.mmall.common.base.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author gg
 * @version AdminController.java, v 0.1 2019-04-30 16:25 gg
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/")
public class AdminController {

    @Resource
    private AdminService adminService;


    /**
     * 登陆
     *
     * @param userLoginParam UserLoginParam
     * @return ServerResponse
     */
    @PostMapping("login")
    public ServerResponse login(UserLoginParam userLoginParam) {
        return ServerResponse.createBySuccess(adminService.login(userLoginParam));
    }

    /**
     * 退出登录
     *
     * @return ServerResponse
     */
    @PostMapping("logout")
    public ServerResponse logout() {
        ShiroUser currentAdmin = ShiroOperation.getCurrentAdmin();
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(403, "请重新登陆");
        }
        SecurityUtils.getSubject().logout();
        ShiroOperation.THREAD_LOCAL.remove();
        return ServerResponse.createBySuccess();
    }


    /**
     * 添加用户
     * 注意 前端传递过来的密码 需要加密 禁止明文传输
     *
     * @param addUserParam AddUserParam
     * @return ServerResponse
     */
    @PostMapping("add")
    public ServerResponse add(@RequestBody @Valid AddUserParam addUserParam) {
        return ServerResponse.createBySuccess(adminService.add(addUserParam));
    }

}